package com.rc.activity;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.OnOpenListener;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.rc.activity.dao.tbl_Trip;
import com.rc.db.DatabaseSupport;
import com.rc.fragment.MenuFragment;
import com.rc.fragment.ReceiptDetailFragment;
import com.rc.fragment.ReceiptFragment;
import com.rc.fragment.ReportFragment;
import com.rc.fragment.TripDetailFragment;
import com.rc.fragment.TripFragment;
import com.rc.object.TripObj;
import com.telpoo.frame.model.BaseModel;
import com.telpoo.frame.model.ModelListener;
import com.telpoo.frame.ui.BaseFragment;

public class ReceiptBox extends SlidingFragmentActivity implements
		ModelListener {
	public BaseModel model = null;
	public TextView tvCenterTitle;
	public RelativeLayout rlTitle;
	public ImageView imgLogo;
	public ProgressDialog loadingProgress;
	private LinearLayout llTitle;
	private int mReceipt;
	public BaseFragment showingFragment = null;
	private int type;
	private Fragment mContent = null;
	private String mTypeText;
	public TextView txtSearch;

	public static void callIntent(Activity ct, String TypeText, int Type) {
		Intent it = new Intent(ct, ReceiptBox.class);
		it.putExtra("type_text", TypeText);
		it.putExtra("type", Type);
		it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		it.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		ct.startActivity(it);
	}

	@SuppressLint("InlinedApi")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		if (getIntent() != null) {
			type = getIntent().getIntExtra("type", 1);
			mTypeText = getIntent().getStringExtra("type_text");
		}
		if (getIntent() != null) {
			switch (type) {
			case 1:
				ReceiptFragment fReceipt = new ReceiptFragment(
						Integer.valueOf(mTypeText), 0, 0);
				mContent = fReceipt;
				txtSearch = fReceipt.txtSearch;
				break;
			case 2:
				TripFragment fTrip = new TripFragment(mTypeText, 0, 0);
				mContent = fTrip;
				txtSearch = fTrip.txtSearch;
				break;
			case 3:
				ReportFragment fReport = new ReportFragment(mTypeText, 0, 0);
				mContent = fReport;
				txtSearch = fReport.txtSearch;
				break;
			case 4:
				mContent = new ReceiptDetailFragment(Long.valueOf(mTypeText),
						4, 0, 0);
				break;
			case 5:
				tbl_Trip objTrip = new TripObj();
				objTrip = DatabaseSupport.getTripByID(Long.valueOf(mTypeText),
						ReceiptBox.this);
				if (objTrip != null) {
					mContent = new TripDetailFragment(objTrip.getType(),
							objTrip.getTripID(), true, 0, "", 0, 0);
				} else {
					mContent = new TripFragment(mTypeText, 0, 0);
				}

				break;
			default:
				break;
			}
		}
		if (model == null) {
			model = new BaseModel();
			model.setModelListener1(ReceiptBox.this);
		}
		// this.registerReceiver(Util.mConnReceiver, new IntentFilter(
		// ConnectivityManager.CONNECTIVITY_ACTION));

		// Add content
		setContentView(R.layout.receipt_content_frame);
		if (mContent != null) {
			showingFragment = (BaseFragment) mContent;
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.content_frame, mContent).commit();
		}

		imgLogo = (ImageView) findViewById(R.id.imgLogo);
		llTitle = (LinearLayout) findViewById(R.id.llTitle);
		tvCenterTitle = (TextView) findViewById(R.id.tvCenterTitle);
		llTitle.setOnClickListener(onClick);
		rlTitle = (RelativeLayout) findViewById(R.id.rlTitle);
		// Add slide menu
		if (mReceipt == 1) {
			setBackground(R.color.background_title_receipt, getResources());
		} else if (mReceipt == 2) {
			setBackground(R.color.background_title_trip, getResources());
		}

		Fragment mMenu = new MenuFragment();
		setBehindContentView(R.layout.receipt_menu_frame);
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.menu_frame, mMenu).commit();

		SlidingMenu sm = getSlidingMenu();
		sm.setShadowWidthRes(R.dimen.shadow_width);
		sm.setShadowDrawable(R.drawable.shadow);
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setFadeDegree(0.35f);
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		sm.setOnOpenListener(new OnOpenListener() {

			@Override
			public void onOpen() {
				if (ReceiptBox.onOpen != null) {
					ReceiptBox.onOpen.doOpen(ReceiptBox.this);
				}

			}
		});

		if (getSupportActionBar() != null) {
			getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	OnClickListener onClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (v.getId() == R.id.llTitle) {
				toggle();
			}
		}
	};
	public static OnOpenSliding onOpen;

	public static OnBackPressedListener obBackPress;

	public interface OnOpenSliding {
		public void doOpen(Activity at);
	}

	public void setOnOpenSliding(OnOpenSliding onOpen) {
		ReceiptBox.onOpen = onOpen;
	}

	public void setTitle(String title) {
		tvCenterTitle.setText(title);
	}

	public void setLogo(int mLogo) {
		imgLogo.setImageResource(mLogo);
	}

	public void setBackground(int color, Resources resources) {
		rlTitle.setBackgroundColor(resources.getColor(color));
	}

	public void setOnBackPressedListener(
			OnBackPressedListener onBackPressedListener) {
		ReceiptBox.obBackPress = onBackPressedListener;
	}

	public interface OnBackPressedListener {
		public void doBack(Activity at);
	}

	@Override
	public void onBackPressed() {

		if (ReceiptBox.obBackPress != null) {
			ReceiptBox.obBackPress.doBack(ReceiptBox.this);
		} else {
			super.onBackPressed();
			DashBoardScreen.callIntent(this);
		}
	}

	@Override
	public Context getContext() {
		// TODO Auto-generated method stub
		return this;
	}

	@Override
	public void onSuccess(int taskType, ArrayList<?> list, String msg) {
		// TODO Auto-generated method stub
		if (showingFragment == null)
			return;
		showingFragment.onSuccess(taskType, list, msg);

	}

	@Override
	public void onFail(int taskType, String msg) {
		// TODO Auto-generated method stub
		if (showingFragment == null)
			return;
		showingFragment.onFail(taskType, msg);
	}

	@Override
	public void onProgress(int taskType, int progress) {
		// TODO Auto-generated method stub

	}

	public void showToast(int msg) {
		Toast.makeText(getBaseContext(), getString(msg), Toast.LENGTH_LONG)
				.show();
	}

	protected void showToast(String msg) {
		Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
	}

	public void showProgressDialog(Context context, String message) {
		closeProgressDialog();

		loadingProgress = new ProgressDialog((ReceiptBox) context);
		loadingProgress.setMessage(message);
		loadingProgress.setCanceledOnTouchOutside(false);
		loadingProgress.show();
	}

	public void closeProgressDialog() {
		if (loadingProgress != null) {
			if (loadingProgress.isShowing())
				loadingProgress.dismiss();
			loadingProgress = null;
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
		showingFragment = null;
	}

}
